package com.example.service;

import com.example.entity.Account;
import com.example.entity.ActivityRecord;
import com.example.mapper.ActivityRecordMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ActivityRecordService {

    @Autowired
    private ActivityRecordMapper activityRecordMapper;

    public List<ActivityRecord> selectAll(ActivityRecord activityRecord) {
        return activityRecordMapper.selectAll(activityRecord);
    }

    public void add(ActivityRecord activityRecord) {
        activityRecord.setApplyTime(LocalDateTime.now().toString());
        activityRecordMapper.add(activityRecord);
    }

    public void delete(Integer id) {
        activityRecordMapper.delete(id);
    }

    public void update(ActivityRecord activityRecord) {
        activityRecordMapper.update(activityRecord);
    }

    public PageInfo<ActivityRecord> selectPage(Integer pageNum, Integer pageSize, ActivityRecord activityRecord) {
        Account currentUser = TokenUtils.getCurrentUser();
        if("USER".equals(currentUser.getRole())){
            activityRecord.setUserId(currentUser.getId());
        }
        PageHelper.startPage(pageNum,pageSize);
        List<ActivityRecord> activityRecords = activityRecordMapper.selectAll(activityRecord);
        return PageInfo.of(activityRecords);
    }
}
